export const HOOKS = [
    "beforeCreate",
    "created",
    "beforeMount",
    "mounted",
    "beforeUpdate",
    "updated",
    "beforeDestory",
    "destoryed"
]

// 策略模式
let starts = {}
starts.data = function (parentVal = {}, childVal) {
    return Object.assign(parentVal, childVal)
}
starts.computed = function () { }
starts.watch = function () { }
starts.methods = function (parentVal={}, childVal) {
    return Object.assign(parentVal, childVal)
}

HOOKS.forEach(hook => {
    starts[hook] = mergeHook
})

// 生命周期函数都走这些逻辑，mergeField函数55行调用这里
function mergeHook(parentVal, childVal) {
    if (childVal) {
        if (parentVal) {
            if(!Array.isArray(parentVal))parentVal=[parentVal]
            return parentVal.concat(childVal)
        } else {
            return [childVal]
        }
    } else {
        return parentVal
    }
}

// new Vue传递的属性是放在parent中的
// 使用Vue.mixin编写的值在child中
export function mergeOptions(parent = {}, child) {
    debugger
    // 存储合并结果的对象
    const options = {}
    // 解析 new Vue 传递的
    for (let key in parent) {
        mergeField(key, 1)
    }

    // 解析Vue.mixin混入的
    for (let key in child) {
        mergeField(key)
    }

    function mergeField(key, isParent) {
        if (starts[key]) {
            options[key] = starts[key](parent[key], child[key])
        } else {
            if (isParent) {
                options[key] = parent[key]
            } else {
                options[key] = child[key]
            }
        }
    }

    return options
}